﻿
using HalconDotNet;
using LeaderCCSLaserUI.Models.VisionModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using ViewROI;

namespace LeaderCCSLaserUI.Models
{
    public class VisionResult
    {
        public int PcsOrder;
        public VResult result;//0:未做模板 1:没找到 2:超限 200:PASS
        public double deltaX;
        public double deltaY;
        public double deltaAngle;
        public HObject r_image;//计算图片
        public HObject r_region;
        public HTuple r_row;//焊接位置
        public HTuple r_column;
        public HTuple r_baserow;//模板位置
        public HTuple r_basecolumn;
        public string r_name;
        public VisionResult()
        {
            PcsOrder = 0;
            result = VResult.Null;
            deltaX = 0;
            deltaY = 0;
            deltaAngle = 0;
        }
    }
    public enum VResult
    { 
        Null,
        NG,
        Limit,
        PASS = 200
    }
    public enum HandEnum
    {
        左,
        右
    }
    public enum BarcodeEnum
    {
        产品码,
        载具码,
        FPC码
    }
    public enum ProductEnum
    {
        产品1,
        产品2,
        产品3,
        产品4
    }
    public enum DispReuslt
    {
        Unkown,
        Pass,
        Ng
    }
    public enum MeasureSelecteEnum
    {
        last,
        first,
        all
    }
    public enum MeasureTransitionEnum
    {
        all,
        negative,
        positive,
        uniform
    }
    public static class MFunc
    {
        [DllImport("halcon.dll")]
        public static extern void HIOCancelDraw();//退出画操作时，调用此函数
        public static string LazerScript(string ip, string cmdstr,int port = 13000)
        {
            try
            {
                using (TcpClient client = new TcpClient())
                {

                    var result = client.BeginConnect(ip, port, null, null);

                    var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));

                    if (!success)
                    {
                        throw new Exception("Failed to connect.");
                    }

                    client.EndConnect(result);
                    
                    // Translate the passed message into ASCII and store it as a Byte array.
                    Byte[] data = System.Text.Encoding.ASCII.GetBytes(cmdstr);

                    // Get a client stream for reading and writing.
                    //  Stream stream = client.GetStream();

                    NetworkStream stream = client.GetStream();

                    // Send the message to the connected TcpServer.
                    stream.Write(data, 0, data.Length);
                    // Buffer to store the response bytes.
                    data = new Byte[256];

                    // String to store the response ASCII representation.
                    string responseData = string.Empty;

                    // Read the first batch of the TcpServer response bytes.
                    int bytes = stream.Read(data, 0, data.Length);
                    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

                    // Close everything.
                    stream.Close();
                    client.Close();
                    return responseData;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return string.Empty;
            }
        }
        public static IDictionary<string, FunctionBase> FunDictionary = new Dictionary<string, FunctionBase>();
    }
}
